//
//  XNItemHeadView.m
//  XINNingOA
//
//  Created by samuel on 2021/7/12.
//

#import "XNItemHeadView.h"
#import "XNTitleAlertView.h"

@interface XNItemHeadView()

@property (nonatomic, weak) UIButton *btn;

@property (nonatomic, strong) NSMutableArray *totalTitleArray;

@property (nonatomic, weak) UILabel *titleLab;

@end

@implementation XNItemHeadView

- (instancetype)initWithTitle:(NSString *)title
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
        self.totalTitleArray = [NSMutableArray array];
        

        
        UILabel *titleLab = [[UILabel alloc] init];
        titleLab.text = title;
        titleLab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        [self addSubview:titleLab];
        self.titleLab = titleLab;
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(25);
            make.top.equalTo(self).offset(5);
            make.height.mas_equalTo(18);
        }];
        
        UILabel *lab = [[UILabel alloc] init];
        lab.backgroundColor = [UIColor colorWithHexString:@"#358AFF"];
        [self addSubview:lab];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLab).offset(-10);
            make.width.mas_equalTo(2);
            make.centerY.equalTo(titleLab);
            make.height.mas_equalTo(15);
        }];
        
        UIButton *btn = [[UIButton alloc] init];
        [btn setImage:[UIImage imageNamed:@"baojing"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(popWarning:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        self.btn = btn;
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLab.mas_right).offset(10);
            make.size.mas_equalTo(CGSizeMake(24, 24));
            make.centerY.equalTo(titleLab);
            make.right.lessThanOrEqualTo(self);
        }];
        
    }
    return self;
}

- (void)setTotalArray:(NSArray *)totalArray
{
    _totalArray = totalArray;
    [self.totalTitleArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.totalTitleArray removeAllObjects];
    __block UILabel *lastLab;
    [totalArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *lab = [[UILabel alloc] init];
        lab.font = [UIFont systemFontOfSize:12];
        lab.text = obj;
        lab.numberOfLines = 0;
        [self addSubview:lab];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLab).offset(-5);
            if (idx == 0) {
                make.top.equalTo(self.titleLab.mas_bottom).offset(8);
            }else{
                make.top.equalTo(lastLab.mas_bottom).offset(5);
            }
            make.right.lessThanOrEqualTo(self).offset(-10);
            if (idx == totalArray.count - 1) {
                make.bottom.equalTo(self).offset(-10);
            }
        }];
        lastLab = lab;
        [self.totalTitleArray addObject:lab];
    }];
    [self layoutIfNeeded];
}

- (void)setShowWarning:(BOOL)showWarning
{
    _showWarning = showWarning;
    if (showWarning) {
        self.btn.hidden = NO;
    }else{
        self.btn.hidden = YES;
    }
    
}

- (void)popWarning:(UIButton *)sender
{
    XNTitleAlertView *alert = [[XNTitleAlertView alloc] init];
    alert.alertContent = self.warningTitle;
    [alert show];
}

@end
